package com.crl.rcdp.sys.controller;

import com.cars.util.bean.BasePage;
import com.cars.util.global.GlobalOperaCode;
import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.rcdp.sys.dto.SysApitypeDTO;
import com.crl.rcdp.sys.service.SysApitypeService;
import com.crl.rcdp.sys.vo.SysApitypeSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description: 系统管理-接口类型API
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020-07-30 22:43:56
 */
@RestController
@RequestMapping("/sys/apitype")
@Api(tags = "系统管理-接口类型")
public class SysApitypeController {

    @Autowired
    private SysApitypeService sysApitypeService;

    /**
     * 接口类型列表
     *
     * @param sysApitypeSearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "接口类型列表")
    public JsonResult list(@RequestBody SysApitypeSearchVO sysApitypeSearchVO) {
        int count = sysApitypeService.count(sysApitypeSearchVO);
        List<SysApitypeDTO> list = sysApitypeService.list(sysApitypeSearchVO);
        BasePage<List<SysApitypeDTO>> basePage = new BasePage(count, list);
        return JsonResultUtil.ok(basePage);
    }

    /**
     * 新增接口类型
     *
     * @param sysApitypeDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增接口类型")
    public JsonResult add(@RequestBody @Valid SysApitypeDTO sysApitypeDTO) {
        int flag = sysApitypeService.add(sysApitypeDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 修改接口类型
     *
     * @param sysApitypeDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改接口类型")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody SysApitypeDTO sysApitypeDTO) {
        int flag = sysApitypeService.update(sysApitypeDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除接口类型
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除接口类型")
    public JsonResult delete(@RequestBody Map<String, Long> map) {
        Long id = map.get("id");
        if (id != null) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        int flag = sysApitypeService.delete(id);
        if (flag == GlobalOperaCode.OPERA_SUCCESS) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 获取单个接口类型
     *
     * @param map
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个接口类型")
    public JsonResult get(@RequestBody Map<String, Long> map) {
        Long id = map.get("id");
        if ( id!=null) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        SysApitypeDTO sysApitypeDTO = sysApitypeService.get(id);
        return JsonResultUtil.ok(sysApitypeDTO);
    }
}
